<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\GoodsClass;
use Redirect;
use Illuminate\Support\Facades\Validator;

class GoodsactionController extends Controller {

    //商品分类首页
    public function index() {
        $goodsClasses = getTree(GoodsClass::all()->toArray());
        return view('admin.goodsaction', ['goodsClasses' => $goodsClasses]);
    }

    public function getAllChildren(Request $request) {
        if ($request->input('display')) {
            $goodsClasses = GoodsClass::where('fid', $request->input('id'))->get()->toArray();
            $childrenIds = array_column($goodsClasses, 'id');
        } else {
            $childrenIds = $this->recursionAllChildren($request->input('id'));
        }
        return $childrenIds;
    }

    public function add_class(Request $request) {
        $messages = [
            'class_name.required' => '请填写分类名称',
            'code.required' => '请填写分类代码',
            //'code.alpha_num' => '分类代码必须为字母或者数字',
            'class_info.required' => '请填写分类描述'
        ];
        $validator = Validator::make($request->all(), [
                    'class_name' => 'required',
                    'code' => 'required',
                    'class_info' => 'required',
                    'fid' => 'required'
                        ], $messages);
        if ($validator->fails()) {
            $error = $validator->errors()->all();
            return ['status' => 0, 'msg' => $error[0]];
        }
        $db = new GoodsClass();
        $db->class_name = $request->input("class_name");
        $db->code = $request->input("code");
        $db->class_info = $request->input("class_info");
        $db->fid = $request->input("fid");
        $db->save();
        return ['status' => 1, 'msg' => '添加成功'];
    }

    public function edit_class(Request $request) {
        $goodsClasses = getTree(GoodsClass::all()->toArray());
        $data = GoodsClass::where('id', $request->input('cid'))->first();
        return view('admin.goods_classedit', ['goodsClasses' => $goodsClasses, 'data' => $data]);
    }

    public function editpost_class(Request $request) {
        $messages = [
            'class_name.required' => '请填写分类名称',
            'code.required' => '请填写分类代码',
            //'code.alpha_num' => '分类代码必须为字母或者数字',
            'class_info.required' => '请填写分类描述'
        ];
        $validator = Validator::make($request->all(), [
                    'class_name' => 'required',
                    'code' => 'required',
                    'class_info' => 'required',
                    'fid' => 'required'
                        ], $messages);
        if ($validator->fails()) {
            $error = $validator->errors()->all();
            return ['status' => 0, 'msg' => $error[0]];
        }
        $id = $request->input("id");
        $class_name = $request->input("class_name");
        $class_code = $request->input("code");
        $class_info = $request->input("class_info");
        $fid = $request->input("fid");
        $db = GoodsClass::find($id);
        $childrenIds = $this->recursionAllChildren($id);
        if (in_array($fid, $childrenIds)) {
            return ['status' => 0, 'msg' => '分类父级不能为自己的子级'];
        }
        if ($fid == $id) {
            return ['status' => 0, 'msg' => '父级不能为自己'];
        }
        $db->class_name = str_replace(' ', '', $class_name);
        $db->class_info = $class_info;
        $db->code = $class_code;
        $db->fid = $fid;
        $db->save();
        return ['status' => 1, 'msg' => '修改成功'];
    }

    public function delete(Request $request) {
        if($request->input('type')==0){
            $goodsClass = GoodsClass::find($request->input('id'));
            if($goodsClass->goods->isEmpty()){
                return ['hasGoods'=>0];
            }else{
                return ['hasGoods'=>1];
            }
        }
        $goodsClass = GoodsClass::find($request->input('id'));
        $goodsClass->goods()->update(['class_id'=>0]);
        GoodsClass::destroy($request->input('id'));
        return ['status' => 1, 'msg' => '删除成功'];
    }

    //递归子分类
    private function recursionAllChildren($id, &$childrenIds = []) {
        $goodsclasses = GoodsClass::where('fid', $id)->get();
        foreach ($goodsclasses as $goodsClass) {
            $childrenIds[] = $goodsClass->id;
            $this->recursionAllChildren($goodsClass->id, $childrenIds);
        }
        return $childrenIds;
    }

}
